home *** CD-ROM | disk | FTP | other *** search
/ BBS in a Box 15 / BBS in a box XV-1.iso / Files / Tele / Pete Johnson / TImport 2.0.3<source>.sit / FileUtils.p < prev    next >
Encoding:
Text File  |  1991-02-09  |  3.1 KB  |  118 lines  |  [TEXT/PJMM]

  1. unit FileUtils;
  2.  
  3. {    Created June 11, 1989, to aid Toolbox file calls.                                        }
  4.  
  5. interface
  6.  
  7.     uses
  8.         Globals;
  9.  
  10.     const
  11.         BUFFSIZE = 32768;
  12.  
  13.     type
  14.         FileBuffer = packed array[1..BUFFSIZE] of char;
  15.         BuffPtr = ^FileBuffer;
  16.         BuffHndl = ^BuffPtr;
  17.  
  18.     var
  19.         InFileLength, InBuffIndex, InputAmount, ImportPos, BufferFilePos: longint;
  20.         InBuffHndl: BuffHndl;
  21.         Leftover: STR255;
  22.  
  23.     function TReadALine (FileRefNum: integer): STR255;
  24.  
  25.     procedure FillBuffer (FileRefNum: integer);
  26.  
  27. implementation
  28.  
  29. { ------------------------------------------------------ }
  30.  
  31.     procedure FillBuffer;
  32.  
  33. {    Fills file buffer with BUFFSIZE characters, or to end of file    }
  34. {    Remember to initialize InBuffHndl before calling!                }
  35.  
  36.         var
  37.             CharCount: longint;
  38.             FileError: OSErr;
  39.  
  40.     begin
  41.         FileError := SetFPos(FileRefNum, fsFromStart, BufferFilePos);
  42.         if InFileLength < (ImportPos + BUFFSIZE) then
  43.             InputAmount := InFileLength - ImportPos
  44.         else
  45.             InputAmount := BUFFSIZE;
  46.         FileError := FSRead(FileRefNum, InputAmount, Ptr(InBuffHndl^));
  47.         InBuffIndex := 1;
  48.         FileError := GetFPos(FileRefNum, BufferFilePos);
  49.     end;
  50.  
  51. { ------------------------------------------------------ }
  52.  
  53.     procedure BreakMsgLine (var MsgTxtString: str255);
  54.  
  55. {    On entry MsgTxtString is MAXMSGLINE or more in length. Looks for natural break and splits    }
  56. {    line into MsgTxtString and Leftover components. If no break found, MsgTxt string is    }
  57. {    MAXMSGLINE long and Leftover is empty.                                                        }
  58.  
  59.         var
  60.             i: integer;
  61.  
  62.     begin
  63.         for i := length(MsgTxtString) downto 1 do
  64.             if MsgTxtString[i] in [SPACE, TAB, ENDLINE] then
  65.                 begin
  66.                     Leftover := copy(MsgTxtString, i + 1, 255);
  67.                     MsgTxtString := copy(MsgTxtString, 1, (i - 1));
  68.                     leave;
  69.                 end;
  70.         if (length(MsgTxtString) > MaxMsgLine) then
  71.             begin
  72.                 Leftover := concat(Leftover, copy(MsgTxtString, succ(MaxMsgLine), 255));
  73.                 MsgTxtString := copy(MsgTxtString, 1, MaxMsgLine);
  74.             end;
  75.         while (Leftover[1] in [SPACE, TAB, ENDLINE]) & (length(Leftover) > 0) do
  76.             Leftover := copy(Leftover, 2, 255)
  77.     end;
  78.  
  79. { ------------------------------------------------------ }
  80.  
  81.     function TReadALine;
  82.  
  83. {    Reads a line of characters from a text file, returns string        }
  84. {    Remember to initialize InBuffHndl before calling!                     }
  85. {    Modified 2/91 to break long lines at MaxMsgLine  characters.    }
  86.  
  87.         var
  88.             LineString: STR255;
  89.  
  90.     begin
  91.         LineString := Leftover;
  92.         Leftover := '';
  93.         while (InBuffHndl^^[InBuffIndex] <> ENDLINE) & (ImportPos < InFileLength) & (length(LineString) < MaxMsgLine) do
  94.             begin
  95.                 LineString := concat(LineString, InBuffHndl^^[InBuffIndex]);
  96.                 InBuffIndex := succ(InBuffIndex);
  97.                 ImportPos := succ(ImportPos);
  98.                 if InBuffIndex > BUFFSIZE then
  99.                     FillBuffer(FileRefNum)
  100.             end;
  101.  
  102.         if ((InBuffHndl^^[InBuffIndex] <> ENDLINE) & (ImportPos < InFileLength)) | (length(LineString) > MaxMsgLine) then        {must be long}
  103.             BreakMsgLine(LineString);
  104.  
  105.         if (InBuffHndl^^[InBuffIndex] = ENDLINE) & (ImportPos < InFileLength) then
  106.             begin
  107.                 InBuffIndex := succ(InBuffIndex);
  108.                 ImportPos := succ(ImportPos);
  109.                 if InBuffIndex > BUFFSIZE then
  110.                     FillBuffer(FileRefNum)
  111.             end;
  112.  
  113.         TReadALine := LineString
  114.     end;
  115.  
  116. { ------------------------------------------------------ }
  117.  
  118. end.    {    Unit    }